package com.itheima.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.itheima.common.PageResult;
import com.itheima.common.ResultData;
import com.itheima.domain.SmsCoupon;
import com.itheima.domain.SmsCouponProductCategoryRelation;
import com.itheima.domain.SmsCouponProductRelation;
import com.itheima.service.SmsCouponHistoryService;
import com.itheima.service.SmsCouponProductCategoryRelationService;
import com.itheima.service.SmsCouponProductRelationService;
import com.itheima.service.SmsCouponService;
import com.itheima.vo.CouponVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Morty
 * @create 2022 11 12 15:24
 */
@RestController
@CrossOrigin(origins = "*")
public class SmsCouponController {

    @DubboReference
    SmsCouponService smsCouponService;

    @DubboReference
    SmsCouponHistoryService couponHistoryService;

    @DubboReference
    SmsCouponProductRelationService productRelationService;

    @DubboReference
    SmsCouponProductCategoryRelationService productCategoryRelationService;

    //根据优惠券名称和类型分页获取优惠券信息
    @GetMapping("/coupon/list")
    public ResultData pageList(String name,Integer pageNum,Integer pageSize,Integer type){
       PageResult pageResult =  smsCouponService.pageByNameAndType(name,pageNum,pageSize,type);
       return ResultData.ok(pageResult,"");

    }
    //获取单个优惠券的详细信息
    @GetMapping("/coupon/{id}")
    public ResultData getById(@PathVariable Integer id){
        CouponVo couponVo = smsCouponService.getInfoById(id);
        return ResultData.ok(couponVo,"");
    }
    //根据优惠券id，使用状态，订单编号分页获取领取记录
    @GetMapping("/couponHistory/list")
    public ResultData pageListHistory(Integer couponId,String orderSn,Integer pageNum,Integer pageSize,Integer userStatus){
       PageResult pageResult =  couponHistoryService.pageByCondition(couponId,orderSn,pageNum,pageSize,userStatus);
       return ResultData.ok(pageResult,"");
    }
    //添加优惠券
    @PostMapping("/coupon/create")
    public ResultData save(@RequestBody CouponVo couponVo){
        SmsCoupon coupon = new SmsCoupon();
        BeanUtil.copyProperties(couponVo,coupon);
        smsCouponService.save(coupon);
        List<SmsCouponProductRelation> productRelations = couponVo.getProductRelations();
        if (ArrayUtil.isNotEmpty(productRelations)){
            for (SmsCouponProductRelation productRelation : productRelations) {
                productRelationService.save(productRelation);
            }
        }
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = couponVo.getProductCategoryRelationList();
        if (ArrayUtil.isNotEmpty(productCategoryRelationList)){
            for (SmsCouponProductCategoryRelation categoryRelation : productCategoryRelationList) {
                productCategoryRelationService.save(categoryRelation);
            }
        }

        return ResultData.ok("","");
    }
    //删除优惠卷
    @PostMapping("/coupon/delete/{id}")
    public ResultData delete(@PathVariable Integer id){
        smsCouponService.deleteById(id);
        return ResultData.ok("","");
    }
    //修改优惠券
    @PostMapping("/coupon/update/{id}")
    public ResultData update(@PathVariable Integer id,@RequestBody CouponVo vo){
        SmsCoupon coupon = new SmsCoupon();
        BeanUtil.copyProperties(vo,coupon);
        smsCouponService.updateById(coupon);
        List<SmsCouponProductRelation> productRelations = vo.getProductRelations();
        if (ArrayUtil.isNotEmpty(productRelations)){
            for (SmsCouponProductRelation productRelation : productRelations) {
                productRelationService.updateById(productRelation);
            }
        }
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = vo.getProductCategoryRelationList();
        if (ArrayUtil.isNotEmpty(productCategoryRelationList)){
            for (SmsCouponProductCategoryRelation categoryRelation : productCategoryRelationList) {
                productCategoryRelationService.updateById(categoryRelation);
            }
        }

        return ResultData.ok("","");
    }
}
